package com.chy.blog_server.modular.web.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chy.blog_server.modular.web.entity.BoInfoSet;
import com.chy.blog_server.modular.web.service.IBoInfoSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：情侣空间基本信息和设置表 controller
 * 作者：chenhuiyan
 * 时间：2020/04/02
 */
@Controller
@RequestMapping(value = "/web/boInfoSet")
public class BoInfoSetController {

    /**
     * 前缀
     */
    private static String PREFIX = "web/boInfoSet/";

    @Autowired
    private IBoInfoSetService boInfoSetService;

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "")
    public String index(){
        return PREFIX + "boInfoSet";
    }

    /**
     * 查询列表
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            List<Map<String, Object>> list = boInfoSetService.getList(); //获取数据列表
            Integer count = boInfoSetService.getBaseMapper().selectCount(new QueryWrapper<BoInfoSet>()); //获取总数
            res.put("code", 0);
            res.put("msg", "获取数据成功");
            res.put("count", count);
            res.put("data", list);
        }catch (Exception e){
            e.printStackTrace();
            res.put("code", 500);
            res.put("msg", "获取数据失败");
        }
        return res;
    }

    /**
     * 跳转添加/编辑页面
     * @return
     */
    @RequestMapping(value = "/addOrEdit")
    public String addOrEdit(String id, Model model){
        model.addAttribute("id", id);
        return PREFIX + "boInfoSet_add";
    }

    /**
     * 保存
     * @param boInfoSet
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Map<String, Object> saveData(BoInfoSet boInfoSet){
        Map<String, Object> res = new HashMap<>();
        try {
            if(StrUtil.isEmpty(boInfoSet.getId())){
                boInfoSetService.save(boInfoSet);
                res.put("state", "200");
                res.put("message", "添加成功");
            }else{
                boInfoSetService.updateById(boInfoSet);
                res.put("state", "200");
                res.put("message", "修改成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "添加失败");
        }
        return res;
    }

    /**
     * 根据id查找信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/getInfoById")
    @ResponseBody
    public Map<String, Object> getInfoById(String id){
        Map<String, Object> res = new HashMap<>();
        try{
            res = boInfoSetService.getInfoById(id);
            res.put("state", "200");
            res.put("message", "获取成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "获取失败");
        }
        return res;
    }

    /**
     * 修改开放状态
     * @param infoSetId
     * @return
     */
    @RequestMapping(value = "/updateIsOpen")
    @ResponseBody
    public Map<String, Object> updateIsOpen(String infoSetId, Integer isOpen){
        Map<String, Object> res = new HashMap<>();
        try{
            BoInfoSet boInfoSet = boInfoSetService.getById(infoSetId);
            boInfoSet.setIsOpen(isOpen);
            boInfoSetService.updateById(boInfoSet);
            res.put("state", "200");
            res.put("message", "修改成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "修改失败");
        }
        return res;
    }

    /**
     * 选择音乐
     * @param musicId
     * @param musicName
     * @return
     */
    @RequestMapping(value = "/chooseMusic")
    public String chooseMusic(Model model, String musicId, String musicName){
        model.addAttribute("musicId", musicId);
        model.addAttribute("musicName", musicName);
        return PREFIX + "boMusicIndex";
    }

}
